package net.hserver.sync.event;

import cn.hserver.core.ioc.annotation.Autowired;
import cn.hserver.core.ioc.annotation.queue.QueueHandler;
import cn.hserver.core.ioc.annotation.queue.QueueListener;
import lombok.extern.slf4j.Slf4j;
import net.hserver.sync.config.EventConst;
import net.hserver.sync.config.LogLevel;
import net.hserver.sync.config.LogType;
import net.hserver.sync.controller.system.log.LogWebSocket;
import net.hserver.sync.entity.SyncLogEntity;
import net.hserver.sync.mapper.SyncLogMapper;

import java.util.Date;

@Slf4j
@QueueListener(queueName = EventConst.LOG_EVENT)
public class LogEvent {

    @Autowired
    private SyncLogMapper syncLogMapper;

    @QueueHandler
    public void save(LogLevel level, LogType type, String message) {
        SyncLogEntity syncLogEntity = new SyncLogEntity();
        syncLogEntity.setLevel(level);
        syncLogEntity.setType(type);
        syncLogEntity.setMessage(message);
        syncLogEntity.setCreateTime(new Date());
        if(syncLogMapper.insert(syncLogEntity)>0){
            LogWebSocket.sendMessage(syncLogEntity);
        }
    }

}
